<?php
/**
 * Created by PhpStorm.
 * User: HLG
 * Date: 2019/10/14
 * Time: 16:54
 */
/**
 * 生成数据的返回值
 *
 * @param unknown $code
 * @param unknown $data
 */
function AjaxReturn($code,$msg='',$data=array())
{
    $returnData = [
        'code'=>$code,
        'msg'=>$msg,
    ];
    if(!empty($data) || $data==0 )
    {
        $returnData["data"]=$data;
    }
    exit(json_encode($returnData));
}

/**
 * 获取系统设置
 *
 * @return mixed
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function getSetting()
{
    $settingModel = new \app\api\model\Setting();
    $data = $settingModel->getSetting();
    return $data;
}

/**
 * 判断是否实名认证
 *
 * @param $user_id
 * @return bool
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function is_authentic($user_id)
{
    $authenticModel = new \app\api\model\Authentic();
    $check = $authenticModel->getInfo(['user_id'=>$user_id,'is_pay'=>1]);
    if($check){
        return true;
    }else{
        AjaxReturn(0,'请先实名认证');
    }
}

/**
 * 判断用户余额是否充足
 *
 * @param $user_id
 * @param $number
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function checkBalance($user_id,$number)
{
   $balance = new \app\api\model\Balance();
    $data = $balance->getInfo(['user_id'=>$user_id]);
    if($data['balance']<$number){
        AjaxReturn(0,'您的HUBE不足');
    }
}

/**
 * 判断是否在活动期间
 *
 * @return bool
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function is_active()
{
    $invitationModel = new \app\api\model\Invitation();
    $current_time = time();
    //取正在进行的活动
    $active = $invitationModel
        ->where('create_time','<= time',$current_time)
        ->where('end_time','>= time',$current_time)
        ->where(['is_end'=>0,'is_deleted'=>0])
        ->find();
    if(empty($active)){
        return false;
    }else{
        return $active['id'];
    }

}

/**
 * 判断是否领取过活动奖励
 *
 * @param $user_id
 * @return bool
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function is_receive($user_id,$active_id)
{
    $invitationLogModel = new \app\api\model\InvitationLog();
    //判断是否领取此活动奖励
    $is_receive = $invitationLogModel->getInfo(['invitation_id'=>$active_id,'user_id'=>$user_id]);
    if($is_receive){
        return false;
    }else{
        return true; //没有领取奖励
    }
}
function suanli($uid,$num=''){
      $arr = [
          'uid'=>$uid,
          'num'=>$num,
          'add_time'=>time()
      ];
      $res = db('sllog')->insert($arr);
      if($res){
          return true;
      }else{
          return false;
      }

    }
/**
 * 计算时间差
 * @param int $timestamp1 时间戳开始
 * @param int $timestamp2 时间戳结束
 * @return array
 */
function time_diff($timestamp1, $timestamp2)
{
    if ($timestamp2 <= $timestamp1)
    {
        return ['hours'=>0, 'minutes'=>0, 'seconds'=>0];
    }
    $timediff = $timestamp2 - $timestamp1;
    // 时
    $remain = $timediff%86400;
    $hours = intval($remain/3600);

    // 分
    $remain = $timediff%3600;
    $mins = intval($remain/60);
    // 秒
    $secs = $remain%60;

    $time = ['hours'=>$hours, 'minutes'=>$mins, 'seconds'=>$secs];

    return $time;
}
